package com.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class MyAspect {

    //切点表达式的抽取
    //需要额外定义方法书写切点注解 该方法就是使用切点表达式的id
    @Pointcut("execution(* com.aop.Target.*(..))")
    public void pointcut(){}


    //通过方法的全路径的形式进行调用
    @Before("com.aop.MyAspect.pointcut()")
    public void before(){
        System.out.println("前置通知");
    }

    @After("execution(* com.aop.Target.*(..))")
    public void after() {
        System.out.println("后置通知");
    }

    @Around("execution(* com.aop.Target.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知");
        Object[] args = joinPoint.getArgs();//获取请求参数
        Object proceed = joinPoint.proceed(args);//调用源方法
        System.out.println("环绕通知");
        return proceed;
    }

    @AfterThrowing(value ="execution(* com.aop.Target.*(..))",throwing = "e")
    public void exception(Exception e) {
        System.out.println(e);
    }

    @AfterReturning(value ="execution(* com.aop.Target.*(..))",returning = "o")
    public void returning(JoinPoint joinPoint, Object o) throws Throwable {
        System.out.println(Arrays.toString(joinPoint.getArgs()));
        System.out.println(o);
    }






}
